import pytz
import datetime
import coin.exchange.base.kr_rest.private_client_base as privcb
import coin.exchange.bithumb.kr_rest_web.native_private_client as nprivc

from coin.exchange.bithumb.kr_rest_web.constants import coin_list, currency_list

import coin.exchange.bithumb.kr_rest.product as exchange_product
from coin.exchange.bithumb.kr_rest.product import BithumbProduct
from coin.exchange.bithumb.kr_rest.currency import to_native_currency
from coin.proto.coin_query_pb2 import (AccountBalance,
                                       CurrencyBalance,
                                       ProductFillElement,
                                       ProductFillBundle,
                                       ProductOrderElement,
                                       ProductOrderBundle)

import coin.proto.coin_order_enums_pb2 as coin_order


def _get_bithumb_datetime_str(timestamp):
  dt = datetime.datetime.fromtimestamp(timestamp, pytz.timezone('Asia/Seoul'))

  return dt.strftime('%Y-%m-%d %H:%M:%S.%f')


def _get_trade_side(json_elem):
  # HACK(wooyaggo): Get order type by misu field.
  if json_elem['BUY_MISU_YN'] == '':
    return coin_order.SELL_ORDER
  elif json_elem['SELL_MISU_YN'] == '':
    return coin_order.BUY_ORDER
  else:
    raise ValueError('Invalid SEARCH_GB: ' + json_elem['SEARCH_GB'])


class BithumbWebPrivateParser(object):
  @staticmethod
  def parse_balance(json_msg) -> AccountBalance:
    assert json_msg['error'] == '0000'

    currency_set_in_json = json_msg['data'].keys()
    currency_set = set(coin_list + ['KRW']) & set(currency_set_in_json)  # Intersection
    currency_balance_list = []
    for currency in currency_set:
      currency_balance = CurrencyBalance(currency=currency,
                                         currency_native=to_native_currency(currency),
                                         total=float(json_msg['data'][currency]['TOTAL']),
                                         available=float(json_msg['data'][currency]['AVAILABLE']),
                                         hold=float(json_msg['data'][currency]['IN_USE']))
      currency_balance_list.append(currency_balance)

    return AccountBalance(exchange="Bithumb", each_balance=currency_balance_list)

  @staticmethod
  def parse_fills(json_msg, product) -> ProductFillBundle:
    raise NotImplementedError()

  @staticmethod
  def parse_orders(json_msg, symbol):
    currency_order_list = []
    for json_elem in json_msg['data']['list']:
      currency_order = ProductOrderElement()
      currency_order.symbol = symbol
      currency_order.price = float(json_elem['CONTRACT_AMT'])

      # TODO(wooyaggo): Need to check difference between 'TOT_REQ_COIN_AMT' and 'QTY2'.
      # Intended for an net quantity.
      currency_order.qty = float(json_elem['QTY2'])
      transaction_time = datetime.datetime.strptime(json_elem['TRAN_DT'] + ' +0900',
                                                    '%Y-%m-%d %H:%M:%S.%f %z')
      currency_order.order_id = str(int(transaction_time.timestamp() * 1e6))
      currency_order.created_timestamp = int(transaction_time.timestamp() * 1e6) * 1000
      currency_order.side = _get_trade_side(json_elem)
      currency_order_list.append(currency_order)

    return ProductOrderBundle(exchange='Bithumb', symbol=symbol, each_order=currency_order_list)


class BithumbWebPrivateClient(privcb.PrivateClientBase):
  ProductType = exchange_product.BithumbProduct

  def __init__(self, cookie_file):
    privcb.PrivateClientBase.__init__(self)
    self.nprivc = nprivc.BithumbWebNativePrivateClient(cookie_file=cookie_file)

  def query_account_balance_impl(self):
    update = self.nprivc.query_account_balance()
    """{'error': '0000', 'message': '', 'data': {'KRW': {'IN_USE': 3348, 'AVAILABLE': 33812, 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': 33812, 'TOTAL': '37160.00000000', 'TOTAL_SUM': '495746.00000000'}, 'BTC': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': '0.00000000', 'DEPO': 0, 'OUT_AVAILABLE': 0, 'LAST': '12455000', 'TOTAL': '0.00000000'}, 'BCH': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '1523000', 'TOTAL': '0.00000000'}, 'BTG': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '138000', 'TOTAL': '0.00000000'}, 'DASH': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '774000', 'TOTAL': '0.00000000'}, 'EOS': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '9830', 'TOTAL': '0.00000000'}, 'ETC': {'IN_USE': '0.000000000000000000', 'AVAILABLE': '0.000000000000000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.000000000000000000', 'LAST': '40500', 'TOTAL': '0.00000000'}, 'ETH': {'IN_USE': '0.000000000000000000', 'AVAILABLE': '0.457792500000000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.457792500000000000', 'LAST': '999000', 'TOTAL': '0.45779250'}, 'LTC': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '248800', 'TOTAL': '0.00000000'}, 'QTUM': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '31400', 'TOTAL': '0.00000000'}, 'XMR': {'IN_USE': '0.000000000000', 'AVAILABLE': '0.000000000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.000000000000', 'LAST': '353100', 'TOTAL': '0.00000000'}, 'XRP': {'IN_USE': '0.000000', 'AVAILABLE': '1.098390', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '1.098390', 'LAST': '1140', 'TOTAL': '1.09839000'}, 'ZEC': {'IN_USE': '0.00000000', 'AVAILABLE': '0.00000000', 'MISU': 0, 'DEPO': 0, 'OUT_AVAILABLE': '0.00000000', 'LAST': '502000', 'TOTAL': '0.00000000'}}}"""
    # TODO(wooyaggo): Do I have to filter by |product|?
    update.msg = BithumbWebPrivateParser.parse_balance(update.msg)
    return update

  def query_fills_impl(self, product):
    raise NotImplementedError()

  def query_submit_impl(self, product, side, price, qty):
    symbol = product.native_symbol_web
    if side == "bid":
      update = self.nprivc.query_submit_buy(symbol, price, qty)
    elif side == "ask":
      update = self.nprivc.query_submit_sell(symbol, price, qty)
    else:
      raise ValueError(side)
    return update

  def query_cancel_impl(self, product, order_id):
    assert order_id.isdigit()

    orders_update = self.query_list_orders(product)
    for order in orders_update.msg.each_order:
      if order.order_id == order_id:
        order_side = None
        if order.side == coin_order.BUY_ORDER:
          order_side = 'buy'
        elif order.side == coin_order.SELL_ORDER:
          order_side = 'sell'
        else:
          raise ValueError(order.side)

        product = BithumbProduct.FromStr(order.symbol)
        return self.nprivc.query_cancel_by_transaction_datetime(
            _get_bithumb_datetime_str(int(order_id) / 1e6), order_side, product.native_symbol_web)

    raise Exception('Could not find the order: ' + order_id)

  def query_cancel_product_impl(self, product):
    return self.nprivc.query_cancel_product(product.native_symbol_web)

  def query_list_orders_impl(self, product):
    update = self.nprivc.query_list_orders(product.native_symbol_web)
    """ {'error': '0000', 'message': '', 'data': {'list': [{'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:55.287082', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:55.287082', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:55', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '149', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909563'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:55.079467', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:55.079467', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:55', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '148', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909562'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:54.559360', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:54.559360', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:54', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '147', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909561'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:54.339335', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:54.339335', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:54', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '146', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909560'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:54.133747', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:54.133747', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:54', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '145', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909559'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:53.912963', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:53.912963', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:53', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '144', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909558'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:53.201647', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:53.201647', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:53', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '143', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909557'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:52.790818', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:52.790818', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:52', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '142', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909555'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:52.473260', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:52.473260', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:52', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '141', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909554'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:52.152360', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:52.152360', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:52', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '140', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909553'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:51.809566', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:51.809566', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:51', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '139', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909551'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:51.480689', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:51.480689', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:51', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '138', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909550'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:51.138929', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:51.138929', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:51', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '137', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909549'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:50.818271', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:50.818271', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:50', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '136', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909548'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:50.599070', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:50.599070', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:50', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '135', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909547'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:50.317783', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:50.317783', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:50', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '134', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909545'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:50.111258', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:50.111258', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:50', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '133', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909544'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:49.897717', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:49.897717', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:49', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '132', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909543'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:49.696293', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:49.696293', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:49', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '131', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909542'}, {'SEARCH_GB': '1P', 'TRAN_GB': '구매대기', 'BUY_MISU_YN': 'N', 'SELL_MISU_YN': '', 'TRANSFER_DATE': '2018-02-22 00:40:49.450187', 'END_DT': '0', 'TRAN_DT': '2018-02-22 00:40:49.450187', 'TRAN_DD': '2018-02-22', 'TRAN_TM': '00:40:49', 'COIN_AMT': '+ 0.0', 'KRW_AMT': '0', 'CONTRACT_AMT': '130', 'FEE': '0.0 COIN', 'COIN_REMAIN': '0.0', 'KRW_REMAIN': '0', 'COLOR': 'red', 'TX_ID': '', 'COIN_IN_ADD': '', 'CONFIRMATIONS': '', 'MISU_YN': 'N', 'MISU_AMT': '', 'MISU_SUM_AMT': '', 'TOT_REQ_COIN_AMT': '1.2', 'QTY2': '1.2', 'CONT_QTY': '0.0', 'STA_CD': 'P', 'AUTH_TYPE': '', 'MARKET_YN': 'N', 'seq': '36909540'}], 'total': '20', 'page': 1}}"""
    update.msg = BithumbWebPrivateParser.parse_orders(update.native_msg, product.symbol)
    return update
